package cn.mogugo.joke.tld;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;
import org.hibernate.util.StringHelper;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.mogugo.joke.manager.JokeObjManager;
import cn.mogugo.joke.model.JokeObj;

public class JokeObjNextAndLastTld  extends BodyTagSupport {

	public String random;
	public Integer allwaysGetNew;
	
	public int doEndTag() throws JspException {
		WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletConfig().getServletContext());
		JokeObjManager jokeObjManager = (JokeObjManager) context.getBean("jokeObjManager");	
		HttpServletRequest hsreq = (HttpServletRequest)pageContext.getRequest();
		HttpServletResponse hsresp = (HttpServletResponse)pageContext.getResponse();
		String uri = hsreq.getRequestURI();
		
		JokeObjDetailTld jokeObjDetailTld = (JokeObjDetailTld) TagSupport.findAncestorWithClass(this, JokeObjDetailTld.class);
		Integer[] nextAndLastJokeObjId = jokeObjDetailTld.nextAndLastJokeObjId;
		
		JokeObj nextJo;
		JokeObj lastJo;
		String type = hsreq.getParameter("type");
		if(null != allwaysGetNew && allwaysGetNew.intValue() == 1){
			if(StringUtils.isEmpty(random) || !random.equals("1")){
				JokeObj curJo = jokeObjDetailTld.jo;
				nextJo = jokeObjManager.getNextAndLastJo(curJo, "next" , type);
				lastJo = jokeObjManager.getNextAndLastJo(curJo, "last" , type);
			}else{
				nextJo = jokeObjManager.getRandom();
				lastJo = jokeObjManager.getRandom();
			}
			jokeObjDetailTld.nextAndLastJokeObjId = new Integer[]{nextJo.getId() , lastJo.getId()};
		}else{
			if(nextAndLastJokeObjId == null){
				if(StringUtils.isEmpty(random) || !random.equals("1")){
					JokeObj curJo = jokeObjDetailTld.jo;
					nextJo = jokeObjManager.getNextAndLastJo(curJo, "next" , type);
					lastJo = jokeObjManager.getNextAndLastJo(curJo, "last" , type);
				}else{
					nextJo = jokeObjManager.getRandom();
					lastJo = jokeObjManager.getRandom();
				}
				jokeObjDetailTld.nextAndLastJokeObjId = new Integer[]{nextJo.getId() , lastJo.getId()};
				
			}else{
				nextJo = jokeObjManager.getById(nextAndLastJokeObjId[0]);
				lastJo = jokeObjManager.getById(nextAndLastJokeObjId[1]);
			}
		}
		


		
		
		
		BodyContent bc = getBodyContent();
		String body = bc.getString();
		if(nextJo != null){
			body = StringHelper.replace(body, "$_jo_next_joke_title" , nextJo.getJoke_title());
			if(StringUtils.isEmpty(random) || !random.equals("1")){
				body = StringHelper.replace(body, "$_jo_next_joke_url" , hsresp.encodeURL(uri + "?type="+type+"&id="+nextJo.getId()));
			}else{
				body = StringHelper.replace(body, "$_jo_next_joke_url" , hsresp.encodeURL(uri + "?id="+nextJo.getId()));
			}
		}else{
			body = StringHelper.replace(body, "$_jo_next_joke_title" , "没有了");
			body = StringHelper.replace(body, "$_jo_next_joke_url" , "javascript:void(0);");
		}

		if(lastJo != null){
			body = StringHelper.replace(body, "$_jo_last_joke_title" , lastJo.getJoke_title());
			if(StringUtils.isEmpty(random) || !random.equals("1")){
				body = StringHelper.replace(body, "$_jo_last_joke_url" , hsresp.encodeURL(uri + "?type="+type+"&id="+lastJo.getId()));
			}else{
				body = StringHelper.replace(body, "$_jo_last_joke_url" , hsresp.encodeURL(uri + "?id="+lastJo.getId()));
			}
		}else{
			body = StringHelper.replace(body, "$_jo_last_joke_title" , "没有了");
			body = StringHelper.replace(body, "$_jo_last_joke_url" , "javascript:void(0);");
		}
        try {
            pageContext.getOut().print(body);
        } catch (IOException e) {
        }
        return this.EVAL_PAGE;
	}
	
	
	public String getRandom() {
		return random;
	}
	public void setRandom(String random) {
		this.random = random;
	}


	public Integer getAllwaysGetNew() {
		return allwaysGetNew;
	}


	public void setAllwaysGetNew(Integer allwaysGetNew) {
		this.allwaysGetNew = allwaysGetNew;
	}
	
	
}
